﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Composite.Regions;

#region namespaces model-view

using EQual.MainModule.Reviews .Views;
using EQual.MainModule.Reviews.Models;

#endregion

using EQual.Infrastructure;

namespace EQual.MainModule.Reviews.Presenters
{
    public class ReviewsController:EQual .Infrastructure .MVPFramework .Presenters .ControllerBase<ReviewsGridView,DataAccesLayer .Review  > , EQual .Infrastructure .IModuleController 
    {
        DataAccesLayer.ReviewsDataAccesLayer accesLayer;
        EQual.MainModule.DetailsContainer containerView = new DetailsContainer ();


        public DataAccesLayer.ReviewsDataAccesLayer AccesLayer
        {
            get { return accesLayer; }
            set { accesLayer = value; }
        }

       IRegionManager _regionManager;

       ReviewPresentationModel  ViewModel;

       public ReviewsController(IRegionManager regionManager):this()
       {
           this._regionManager = regionManager;
       }

       public ReviewsController()
       {
           accesLayer = new DataAccesLayer.EQualDataLayerFactory().GetReviewsAccesLayer ();
       }

       //secondary Presenters and Views 
       DetailsViews.ReviewParticipantsPresenter participants_presenter;
     //  DetailsViews.EmployeeDistributionPresenter distributionPresenter;
       DetailsViews.ReviewDecisionsPresenter decisions_presenter;

       Presenters.ReviewProjectsPresenter projects_presenter;

       DetailsViews.ReviewsDetailsView detailsView;

       public void Run()
       {
           ViewModel = new ReviewPresentationModel();
           View = new ReviewsGridView();
           detailsView = new EQual.MainModule.Reviews.DetailsViews.ReviewsDetailsView();

           ViewModel.View = View;
           View.Model = ViewModel;
         
           View.ItemSelected += new EventHandler<Microsoft.Practices.Composite.Events.DataEventArgs<object >>(View_ItemSelected);
           View.ItemDeleted += new EventHandler<Microsoft.Practices.Composite.Events.DataEventArgs<object>>(View_ItemDeleted);
           View.ItemAdded += new EventHandler<Microsoft.Practices.Composite.Events.DataEventArgs<object>>(View_ItemAdded);

           participants_presenter = new EQual.MainModule.Reviews.DetailsViews.ReviewParticipantsPresenter();
           decisions_presenter = new EQual.MainModule.Reviews.DetailsViews.ReviewDecisionsPresenter();
           projects_presenter = new ReviewProjectsPresenter();

           this._regionManager.RegisterViewWithRegion(RegionNames.MainRegion,() => this.View);
           this._regionManager.Regions[RegionNames.MainRegion].Activate(this.View);
       }

       void View_ItemAdded(object sender, Microsoft.Practices.Composite.Events.DataEventArgs<object> e)
       {
          RequestNewItemCreation(e.Value);
       }

       private void RequestNewItemCreation(object review)
       {
           this.AccesLayer.AddNewReview(review);
       }

       void View_ItemSelected(object sender, Microsoft.Practices.Composite.Events.DataEventArgs<object> e)
       {
           InjectViews();
           if (e.Value == null) return;

           participants_presenter.ActiveItem = e.Value;
           decisions_presenter.ActiveItem = e.Value;
           projects_presenter.ActiveItem = e.Value;
           detailsView.ActiveItem = e.Value;

       }

       void View_ItemDeleted(object sender, Microsoft.Practices.Composite.Events.DataEventArgs<object> e)
       {
          // RequestItemDelete(e.Value);
       }

     //  DetailsViews.EmployeeDetailedControl   dff = new DetailsViews.EmployeeDetailedControl ();
       protected virtual void InjectViews()
       {
           //throw in to the datails region the datails view
           IRegion region = _regionManager.Regions[RegionNames.ActionRegion];

           if (!region.Views.Contains(containerView))
           {
               containerView.AddView(GetDecoratedView(participants_presenter.View, "Συμετέχοντες"));
               containerView.AddView(GetDecoratedView(decisions_presenter .View , "Θέματα - Αποφάσεις"));
               containerView.AddView(GetDecoratedView(projects_presenter.View, "Projects"));
               containerView.AddView(GetDecoratedView(detailsView, ""));
               //   containerView.AddView(GetDecoratedView(distributionPresenter.View,"Δίανομή"));
               //   containerView.AddView(GetDecoratedView(dff, "Λεπτομέρειες"));
               region.Add(containerView);
           }

           //activate the current view
           region.Activate(containerView);
       }

       public object GetDecoratedView(object view,string header)
       {
           return new DataGridDecorator(view, header);
       }

    }
}
